<?php
namespace app\admin\controller;
use app\admin\model\JsTreasure;
use app\admin\model\JsTreasureMission;

class Tools extends Common{
    public function __construct(){}

    /*藏宝汇总*/
    public function treasureReport(){
        $tbModel = new JsTreasure();
        //藏宝总和
        $totalIncome = $tbModel->sumData([],'Amount');
        //宝箱总数
        $totalBox = $tbModel->getTotalNum([]);
        $totalBox = $totalBox*40;
        //已开宝箱总数
        $totalOpen = $tbModel->sumData([],'ComplateNum');
        //挖宝总和
        $totalOut = $totalIncome*2/$totalBox*$totalOpen;
        $data['totalIncome'] = round($totalIncome/10000,3);
        $data['totalOut'] = round($totalOut/10000,3);
        return json($data);
    }
    /*挖宝趋势统计*/
    public function treasureFindAnaly(){
        $type = $_POST['type'];
        $data = [];
        $total = 0;
        if($type=='next'){
            //未来7天
            $stime = date('Y-m-d H:i:s');
            $etime = date('Y-m-d H:i:s',strtotime('+6 days'));
            $tModel = new JsTreasure();
            $list = $tModel->queryData("select LockOn,Amount from treasure where LockOn>='{$stime}' and LockOn <= '{$etime}'");
            foreach($list as $val){
                $date = date('m/d',strtotime($val['LockOn']));
                $money = $val['Amount']/20;
                $data[$date] += $money;
                $total += $money;
            }
        }elseif($type=='prev'){
            //过去7天
            $stime = date('Y-m-d',strtotime('-6 days'));
            $etime = date('Y-m-d 23:59:59');
            $tmModel = new JsTreasureMission();
            $list = $tmModel->queryData("select CreateOn,Amount,Times from treasuremission where CreateOn>='{$stime}' and CreateOn <= '{$etime}'");
            foreach($list as $val){
                $date = date('m/d',strtotime($val['CreateOn']));
                $money = $val['Amount']*$val['Times'];
                $data[$date] += $money;
                $total += $money;
            }
        }
        $xdata = $ydata = [];
        foreach($data as $key=>$val){
            $xdata[] = $key;
            $ydata[] = $val;
        }
        $res = [];
        if($type=='next'){
            $title = '未来七天挖宝总金额为：<em class="color-blue">'.$total.'</em>';
        }else{
            $title = '过去七天挖宝总金额为：<em class="color-blue">'.$total.'</em>';
        }
        $res['title'] = $title;
        $res['xdata'] = $xdata;
        $res['ydata'] = $ydata;
        return json($res);
    }
    /*藏宝数据分时汇总*/
    public function treasureAnaly(){
        $type = $_POST['type'];
        if($type=='month'){
            //最新半年
            $stime = date('Y-m-d',strtotime('-6 months'));
            $etime = date('Y-m-d 23:59:59');
        }elseif($type=='date'){
            //最近一个月
            $stime = date('Y-m-d',strtotime('-1 months'));
            $etime = date('Y-m-d 23:59:59');
        }
        $tsModel = new JsTreasure();
        $uList = $tsModel->queryData("select CreateOn,Amount from treasure where CreateOn>='{$stime}' and CreateOn <= '{$etime}'");
        $data = [];
        $total = 0;
        if($type=='month'){
            foreach($uList as $val){
                $date = date('Y/m',strtotime($val['CreateOn']));
                $data[$date] += $val['Amount'];
                $total += $val['Amount'];
            }
        }elseif($type=='date'){
            foreach($uList as $val){
                $date = date('m/d',strtotime($val['CreateOn']));
                $data[$date] += $val['Amount'];
                $total += $val['Amount'];
            }
        }
        $xdata = $ydata = [];
        foreach($data as $key=>$val){
            $xdata[] = $key;
            $ydata[] = $val;
        }
        $res = [];
        $res['title'] = date('m月d日',strtotime($stime)).'~'.date('m月d日',strtotime($etime)).'藏宝总金额为：<em class="color-blue">'.$total.'</em>';
        $res['xdata'] = $xdata;
        $res['ydata'] = $ydata;
        return json($res);
    }
}